package project3cs424.src;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.geonames.InvalidParameterException;
import org.geonames.Toponym;
import org.geonames.ToponymSearchCriteria;
import org.geonames.ToponymSearchResult;
import org.geonames.WebService;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;

public class MilitaryScraper {

	static String baseUrl="http://benefits.military.com/misc/installations/";
	static BufferedWriter outputWriter;
	static ToponymSearchCriteria searchCriteria;
	public static void main(String[] args)throws Exception
	{
		String inputUrl="http://benefits.military.com/misc/installations/Browse_Location.jsp";
		searchCriteria=new ToponymSearchCriteria();
		WebService.setUserName("viper11");
		searchCriteria.setCountryCode("US");
		
		outputWriter=new BufferedWriter(new FileWriter(new File("data/military.data")));
		URL url=new URL(inputUrl);
		
		InputStreamReader isr=new InputStreamReader(url.openStream());
		Tidy tidy=new Tidy();
		tidy.setQuiet(true);
		tidy.setShowWarnings(false);
		tidy.setForceOutput(true);
		
		Document document=tidy.parseDOM(isr, null);
		
		NodeList tableNodes=document.getElementsByTagName("body");
		Node requiredBodyNode=tableNodes.item(0);
				
		NodeList requiredBodyNodeChildren=requiredBodyNode.getChildNodes();
		
		Node requiredTableNode=requiredBodyNodeChildren.item(requiredBodyNodeChildren.getLength()-1);
		
		
		Node trNode=requiredTableNode.getFirstChild();
		Node tdNode=trNode.getFirstChild();
		NodeList tdNodeChildren=tdNode.getChildNodes();
		
		Node formNode=tdNodeChildren.item(3);
		NodeList formNodeChildren=formNode.getChildNodes();
		
		Node contentTable=formNodeChildren.item(2);
		NodeList contentTableChildren=contentTable.getChildNodes();
		Node contentTr=contentTableChildren.item(1);
		NodeList contentTrChildren=contentTr.getChildNodes();
		for(int j=0;j<contentTrChildren.getLength();j++)
		{
			Node contentTd=contentTrChildren.item(j);
			if(contentTd.getNodeName().equals("td"))
			{
				NodeList contentTdChildren=contentTd.getChildNodes();
				for(int i=0;i<contentTdChildren.getLength();i++)
				{
					System.out.println(contentTdChildren.item(i).getNodeName()+" "+i);
					processTag(contentTdChildren.item(i));
				}	
			}
			
	
		}
		
		
				
		outputWriter.close();
	}

	static void processTag(Node node) throws Exception
	{
		
		NodeList nodeChildren=node.getChildNodes();
		for(int i=0;i<nodeChildren.getLength();i++)
		{
			Node nodeChild=nodeChildren.item(i);
			//System.out.println(nodeChildren.item(i).getNodeName());
			if(nodeChild.getNodeName().equals("a"))
			{
				NamedNodeMap aAttributes=nodeChild.getAttributes();
				Node hrefAttr=aAttributes.getNamedItem("href");
				String relativeUrl=hrefAttr.getNodeValue();
				//String cityAndState=getCityAndState(baseUrl+relativeUrl);
				Node anchorText=nodeChild.getFirstChild();
				System.out.println(anchorText.getNodeValue());
				String militaryBase=anchorText.getNodeValue();
				searchCriteria.setQ(militaryBase);
				ToponymSearchResult searchResults=WebService.search(searchCriteria);
				String googleApi="http://maps.googleapis.com/maps/api/geocode/xml?address=";
				URL requestUrl=new URL(googleApi+militaryBase.replaceAll(" ", "+"));
			
				
				double latitude;
				double longitude;
				if(searchResults.getTotalResultsCount()>0)
				{
					List<Toponym> results=searchResults.getToponyms();
					latitude=results.get(0).getLatitude();
					longitude=results.get(0).getLongitude();
				}
				else
				{
					latitude=0;
					longitude=0;
				}
				if(militaryBase.equalsIgnoreCase("back to top"))
					continue;
				outputWriter.write(militaryBase+"\n");
			}
		}
		
	}
	
	static String getCityAndState(String url)
	{
		String cityAndState="";
		try {
			URL processUrl=new URL(url);
			InputStreamReader isr;
			isr = new InputStreamReader(processUrl.openStream());
			
			Tidy tidy=new Tidy();
			tidy.setQuiet(true);
			tidy.setShowWarnings(false);
			tidy.setForceOutput(true);
			
			Document document=tidy.parseDOM(isr, null);
			
			Node bodyNode=document.getElementsByTagName("body").item(0);
			NodeList bodyChildren=bodyNode.getChildNodes();
			int tableCount=0;
			Node tableNode=null;
			for(int i=0;i<bodyChildren.getLength();i++)
			{
				//System.out.println(bodyChildren.item(i).getNodeName());
				if(bodyChildren.item(i).getNodeName().equals("table"))
				{
					tableCount++;
					if(tableCount==2)
					{
						tableNode=bodyChildren.item(i);
					}
				}				
			}			
			System.out.println(tableNode.getNodeName());
			
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return cityAndState;	
		
	}

}
